package com.example.administrator.xiaoxiang.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.widget.RadioGroup;

import com.example.administrator.xiaoxiang.R;
import com.example.administrator.xiaoxiang.fragment.ChoiceFragment;
import com.example.administrator.xiaoxiang.fragment.HomeFragment;
import com.example.administrator.xiaoxiang.fragment.PersonalFragment;
import com.example.administrator.xiaoxiang.fragment.RebateFragment;
import com.example.administrator.xiaoxiang.fragment.XiaoXiangDaiFragment;

import java.lang.reflect.InvocationTargetException;

public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener{
    private RadioGroup radioGroup;
    private FragmentManager fragmentManager;
    private FragmentTransaction fragmentTransaction;
    private Fragment currentFragment;
    private AlertDialog.Builder builder;
    private AlertDialog alertDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        init();
        radioGroup.setOnCheckedChangeListener(this);
    }


    private void init() {
        radioGroup = (RadioGroup) findViewById(R.id.radioGroup_main_activity);
        fragmentManager = getSupportFragmentManager();
        radioGroup.getChildAt(0).performClick();
        showFragment(HomeFragment.TAG, HomeFragment.class);
    }

    //设置当前显示的界面
    private void showFragment(String tag, Class<? extends Fragment> className) {

        fragmentTransaction = fragmentManager.beginTransaction();
        if (currentFragment != null) {
            fragmentTransaction.hide(currentFragment);
        }
        currentFragment = fragmentManager.findFragmentByTag(tag);
        if (currentFragment != null) {
//            Log.e("dddd", "showFragment: "+"执行了");
            fragmentTransaction.show(currentFragment);
        } else {
            try {
                currentFragment = className.getConstructor().newInstance();
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            }
            fragmentTransaction.add(R.id.fragment_container_main, currentFragment, tag);
        }
        fragmentTransaction.commit();
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.rb_1:
                showFragment(HomeFragment.TAG, HomeFragment.class);
                break;
            case R.id.rb_2:
                showFragment(ChoiceFragment.TAG, ChoiceFragment.class);
                break;
            case R.id.rb_3:
                showFragment(XiaoXiangDaiFragment.TAG, XiaoXiangDaiFragment.class);
                break;
            case R.id.rb_4:
                showFragment(RebateFragment.TAG, RebateFragment.class);
                break;
            case R.id.rb_5:
                showFragment(PersonalFragment.TAG, PersonalFragment.class);
                break;
        }
    }

    // 退出
//    @Override
//    public void onBackPressed() {
//        builder = new AlertDialog.Builder(this);
//        alertDialog = builder.create();
//        View view = LayoutInflater.from(this).inflate(R.layout.item_back, null, false);
//        alertDialog.setView(view);
//        if (alertDialog != null) {
//            alertDialog.show();
//        }
//    }
//    public void click(View view) {
//        switch (view.getId()) {
//            case R.id.btn1:
//                if (alertDialog != null) {
//                    alertDialog.hide();
//                }
//                break;
//            case R.id.btn2:
//            case R.id.btn3:
//                finish();
//                break;
//        }
//    }
}
